package com.kanshu.ksgb.fastread.doudou.common.business.pay;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.util.StorageUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVDefaultManager;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.bean.OrderRequestParams;

import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 支付失败对话框
 * Created by wcy on 2017\8\1 0001.
 */
public class PayErrorDialog extends Dialog {
    private Activity mActivity;

    public PayErrorDialog(Context context) {
        super(context, R.style.dialog_style);
        mActivity = (Activity) context;
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_pay_error);
        ButterKnife.bind(this);
    }

    private void pay() {
        String payInfo = MMKVDefaultManager.getInstance().getPayInfo();
        if (TextUtils.isEmpty(payInfo)) return;
        String[] info = payInfo.split("#");
        int coin = Integer.parseInt(info[0]);
        int totalFee = Integer.parseInt(info[1]);
        int payType = Integer.parseInt(info[2]);
        int bussninessType = 0;
        if (info.length > 3) {
            bussninessType = Integer.parseInt(info[3]);
        }
        OrderRequestParams params = new OrderRequestParams();
        params.coin = coin;
        params.total_fee = totalFee;
        params.pay_type = payType;
        params.business_type = bussninessType;
        params.book_id = StorageUtils.getPreference(getContext(), Constants.SP_NAME, "charge_book_id", "");
        if (payType == Constants.PayType.TYPE_ALI) {
            AliPayHelper helper = new AliPayHelper(mActivity);
            helper.payAction(params);
        } else if (payType == Constants.PayType.TYPE_WX) {
            WxPayHelper helper = new WxPayHelper(mActivity);
            helper.payAction(params);
        }
        dismiss();
    }

    public static PayErrorDialog show(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        PayErrorDialog dlg = new PayErrorDialog(activity);
        dlg.setCanceledOnTouchOutside(false);
        dlg.show();
        return dlg;
    }

    @OnClick({R.id.tv_confirm, R.id.tv_cancel})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                pay();
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }
}
